package zd.cornermemory.utils;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by Admin on 2023/3/23.
 */

public class PartitionProbabilitiesUtil {
    public static ArrayList<ArrayList<Integer>> getPartition(int n) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if (n == 0) {
            result.add(new ArrayList<Integer>());
            return result;
        }
        for (int i = 1; i <= n; i++) {
            ArrayList<ArrayList<Integer>> subPartitions = getPartition(n - i);
            for (ArrayList<Integer> partition : subPartitions) {
                if (!partition.isEmpty() && i < partition.get(0)) {
                    continue;
                }
                partition.add(0, i);
                result.add(partition);
            }
        }
        return result;
    }

    public static int factorial(int n) {
        if (n == 0) {
            return 1;
        } else {
            return n * factorial(n - 1);
        }
    }

    /**
     * 判断有无奇偶
     * @param partition
     * @param n
     * @return
     */
    public static boolean isParity(ArrayList<Integer> partition, int n) {
        if (partition.size() % 2 == n % 2) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 计算编码长度
     * @param partition
     * @param isBufferFinished 0缓冲不归位 1缓冲归位
     * @return
     */
    public static int getCodeLength(ArrayList<Integer> partition, int isBufferFinished, int n) {
        //编码长度
        int codeLength = n + partition.size() - 2 * Collections.frequency(partition, 1) + 2 * isBufferFinished - 2;
        return codeLength;
    }

    /**
     * 计算小循环数量
     * @param partition
     * @param isBufferFinished 0缓冲不归位 1缓冲归位
     * @return
     */
    public static int getCirculateCount(ArrayList<Integer> partition, int isBufferFinished) {
        //小循环数
        int circulate = partition.size() - Collections.frequency(partition, 1) + isBufferFinished - 1;
        return circulate;
    }
}
